package com.chenyun.system.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.chenyun.common.annotation.Log;
import com.chenyun.common.annotation.Page;
import com.chenyun.common.enums.operlog.BusinessTypeEnum;
import com.chenyun.system.model.SysRole;
import com.chenyun.system.model.SysUser;
import com.chenyun.system.model.SysUserRole;
import com.chenyun.system.service.ISysRoleService;
import com.chenyun.system.service.ISysUserService;
import com.chenyun.boot.web.controller.BaseController;
import com.chenyun.common.constants.UserConstants;
import com.chenyun.common.vo.AjaxResult;
import com.chenyun.common.vo.PageResult;
import com.chenyun.satoken.starter.annotation.SaCheckAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色信息-admin")
@SaCheckAdmin
public class SysRoleController extends BaseController<ISysRoleService, SysRole> {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService userService;

    @ApiOperation("列表")
    @GetMapping("/list")
    @SaCheckPermission("system:role:list")
    @Page
    public PageResult list(SysRole role) {
//        startPage();
        List<SysRole> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }


    /**
     * 根据角色编号获取详细信息
     */
    @ApiOperation("根据角色编号获取详细信息")
    @GetMapping(value = "/{roleId}")
    @SaCheckPermission("system:role:info")
    public AjaxResult getInfo(@PathVariable Long roleId) {
        roleService.checkRoleDataScope(roleId);
        return AjaxResult.SUCCESS(roleService.selectRoleById(roleId));
    }

    /**
     * 新增角色
     */
    @ApiOperation("新增角色")
    @PostMapping
    @Log(title = "新增角色", businessType = BusinessTypeEnum.INSERT)
    @SaCheckPermission("system:role:add")
    public AjaxResult add(@Validated @RequestBody SysRole role) {
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return AjaxResult.FAIL("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            return AjaxResult.FAIL("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
//        role.setCreateBy(SecurityUtils.getUsername());
        return toAjax(roleService.insertRole(role));

    }

    /**
     * 修改保存角色
     */
    @ApiOperation("修改保存角色")
    @PostMapping("edit")
    @Log(title = "修改角色", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:role:edit")
    public AjaxResult edit(@Validated @RequestBody SysRole role) {
        roleService.checkRoleAllowed(role);
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return AjaxResult.FAIL("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            return AjaxResult.FAIL("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
//        role.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(roleService.updateRole(role));
    }

    /**
     * 修改保存数据权限
     */
    @ApiOperation("修改保存数据权限")
    @PostMapping("/dataScope")
    @Log(title = "修改数据权限", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:role:dataScope")
    public AjaxResult dataScope(@RequestBody SysRole role) {
        roleService.checkRoleAllowed(role);
        return toAjax(roleService.authDataScope(role));
    }

    /**
     * 状态修改
     */
    @ApiOperation("状态修改")
    @PostMapping("/changeStatus")
    @Log(title = "修改角色状态", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:role:changeStatus")
    public AjaxResult changeStatus(@RequestBody SysRole role) {
        roleService.checkRoleAllowed(role);
//        role.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(roleService.updateRoleStatus(role));
    }

    /**
     * 删除角色
     */
    @ApiOperation("删除角色")
    @PostMapping("/del")
    @Log(title = "删除角色", businessType = BusinessTypeEnum.DELETE)
    @SaCheckPermission("system:role:del")
    public AjaxResult remove(@RequestBody Long[] roleIds) {
        return toAjax(roleService.deleteRoleByIds(roleIds));
    }

    /**
     * 获取角色选择框列表
     */
    @ApiOperation("获取角色选择框列表")
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        return AjaxResult.SUCCESS(roleService.selectRoleAll());
    }

    /**
     * 查询已分配用户角色列表
     */
    @ApiOperation("查询已分配用户角色列表")
    @GetMapping("/authUser/allocatedList")
    public PageResult allocatedList(SysUser user) {
//        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        return getDataTable(list);
    }

    /**
     * 查询未分配用户角色列表
     */
    @ApiOperation("查询未分配用户角色列表")
    @GetMapping("/authUser/unallocatedList")
    public PageResult unallocatedList(SysUser user) {
//        startPage();
        List<SysUser> list = userService.selectUnallocatedList(user);
        return getDataTable(list);
    }

    /**
     * 取消授权用户
     */
    @ApiOperation("取消授权用户")
    @PostMapping("/authUser/cancel")
    @Log(title = "取消角色授权用户", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:role:cancelAuthUser")
    public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole) {
        return toAjax(roleService.deleteAuthUser(userRole));
    }

    /**
     * 批量取消授权用户
     */
    @ApiOperation("批量取消授权用户")
    @PostMapping("/authUser/cancelAll")
    @Log(title = "批量取消角色授权用户", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:role:cancelAuthUserAll")
    public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds) {
        return toAjax(roleService.deleteAuthUsers(roleId, userIds));
    }

    /**
     * 批量选择用户授权
     */
    @ApiOperation("批量选择用户授权")
    @PostMapping("/authUser/selectAll")
    @Log(title = "批量选择用户角色授权", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:role:selectAuthUserAll")
    public AjaxResult insertAuthUsers(Long roleId, Long[] userIds) {
        return toAjax(roleService.insertAuthUsers(roleId, userIds));
    }

    @ApiOperation("为角色授权菜单")
    @PostMapping("/authMenu")
    @Log(title = "为角色授权菜单", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:role:authMenu")
    public AjaxResult authMenu(Long roleId, @RequestBody Long[] menuIds) {
        return toAjax(roleService.authMenu( roleId,menuIds));
    }


}